package com.serkol.annotation;

import com.serkol.constant.AuthConstant;
import com.serkol.constant.ResultCode;
import com.serkol.exception.AuthException;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.List;

@Aspect
@Component
public class AuthAspect {
    private ThreadLocal<SimpleDateFormat> format = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));

    //切点表达式，表示加了Auth注解的都是切点，路径是自定义注解的全路径
    @Pointcut("@annotation(Auth)")
    public void pointcut() {

    }

    @Around("@annotation(auth)")
    public Object operationLogRecord(ProceedingJoinPoint joinPoint, Auth auth) throws AuthException {

        List<String> auth1 = AuthConstant.getAuth();
        boolean contains = auth1.contains(auth.value());
        if(!contains) throw new AuthException(ResultCode.REQUEST_ERROR_UNAUTHORIZED);
      //  System.out.println("Auth:"+ contains);
        //响应
        Object responseResult = null;
        System.out.println("start 原方法报错");
        try {
            //返回切点处继续执行原方法，并接收原方法的响应
            responseResult = joinPoint.proceed();

        } catch (Throwable e) {
            e.printStackTrace();
            System.out.println("1原方法报错，不需要记录日志");
        }


        System.out.println("保存操作日志出错");


        return responseResult;
    }

}